package com.choudou5.solr.utils;

import cn.hutool.core.collection.CollUtil;
import com.choudou5.solr.bean.sys.SysDictBean;
import com.choudou5.solr.service.ServiceHelper;
import com.choudou5.solr.service.sys.SysDictService;
import com.choudou5.solr.util.JsonUtil;
import org.apache.commons.collections.MapUtils;

import java.util.List;
import java.util.Map;

/**
 * @name 系统字典
 * @author choudou5
 * @date 2018-08-06
 */
public class SysDictUtil {

    private static SysDictService sysDictService = ServiceHelper.singleton(SysDictService.class);


    public static List<SysDictBean> getListByType(String dictType){
        return sysDictService.getListByType(dictType);
    }

    public static SysDictBean getOneByType(String dictType){
        List<SysDictBean> list = sysDictService.getListByType(dictType);
        return CollUtil.isNotEmpty(list)?list.get(0):null;
    }

    public static List<String> getValueListByType(String dictType){
        return sysDictService.getListValueByType(dictType);
    }

    public static String getOneValueByType(String dictType){
        List<String> list = sysDictService.getListValueByType(dictType);
        return CollUtil.isNotEmpty(list)?list.get(0):null;
    }

    public static String getOneJsonValueByType(String dictType, String jsonKey){
        Map<String, String> map = getOneJsonValueByType(dictType);
        return map.get(jsonKey);
    }

    public static Map<String, String> getOneJsonValueByType(String dictType){
        List<String> list = sysDictService.getListValueByType(dictType);
        return CollUtil.isNotEmpty(list)? JsonUtil.toJavaObj(list.get(0), Map.class): MapUtils.EMPTY_MAP;
    }

    public static void insertDictVal(String title, String dictType, String dictVal) {
        sysDictService.insertDictVal(title, dictType, dictVal);
    }

}
